package com.heifeng.cloudshelf.utils;

import android.content.Context;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

public class RichTextUtils {

//    private static Richtext richtext;
//    private static Context mContext;
    private Object object;

    public static RichTextUtils getInstance(Context context, @NonNull Object object) {
//        WeakReference<Context> contextWeakReference = new WeakReference<Context>(context);
//        mContext = contextWeakReference.get();
        RichTextUtils utils = new RichTextUtils(context);
        utils.setObject(object);
        return utils;
    }

    private RichTextUtils(Context context) {
        initCacheDir(context);
    }

    public void setObject(Object object) {
        this.object = object;
    }

    /**
     * 初始化RichText
     * */
    public void initCacheDir(Context context){
        RichText.initCacheDir(context);
    }

    /**
     * 显示富文本内容
     * */
    public void setRichText(String content, TextView textView){
        RichText.fromHtml(content).bind(object)
                .showBorder(false)
                .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
                .into(textView);
    }

    /**
     * 释放资源
     * */
    public void destory(){
        RichText.clear(object);
    }
}
